<template>
  <div>
    <div style="margin-bottom: 10px"><span>货代管理</span></div>
    <div
      style="
        display: flex;
        align-items: center;
        height: 60px;
        background-color: #fff;
        border-radius: 5px;
      "
    >
      <div class="container">
        <el-form :model="form" label-width="auto" style="max-width: 700px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="货代公司名称:">
                <el-input v-model="form.companyName" /> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="联系方式:">
                <el-input v-model="form.contactTel" /> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="邮箱:">
                <el-input v-model="form.email" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-button type="primary" style="margin-left: 15px" @click="query"
          >查询</el-button
        >
        <el-button type="primary" style="margin-left: 15px" @click="reset"
          >重置</el-button
        >
      </div>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" plain @click="add">添加</el-button>
    </div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="160" />
      <el-table-column prop="company_name" label="货代公司名称" width="160" />
      <el-table-column prop="contact" label="联系人" width="160" />
      <el-table-column prop="contact_tel" label="联系方式" width="160" />
      <el-table-column prop="email" label="邮箱" width="180" />
      <el-table-column prop="create_time" label="操作时间" width="160" />
      <el-table-column label="操作" header-align="center" align="center">
        <template v-slot="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            title="修改"
            @click="update(row.id)"
        /></template>
      </el-table-column>
    </el-table>
    <div
      style="
        display: flex;
        justify-content: center;
        height: 50px;
        margin-top: 20px;
        line-height: 50px;
        text-align: right;
      "
    >
      <el-pagination
        background
        :page-size="pageSize"
        :page-sizes="[2, 5, 10, 20, 50]"
        layout="total,sizes,prev, pager, next,jumper"
        :total="total"
        @size-change="chgsize"
        @current-change="chgpg"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { Edit } from "@element-plus/icons-vue";
import { queryForward } from "@/api/forward/forward.js";
import { onMounted } from "vue";
import { useRouter } from "vue-router";
const page = ref(1); //当前页 默认第1页
const pageSize = ref(5); //每页记录数
const total = ref(0); //总记录数
const router = useRouter();
function add() {
  router.push("/forward/add");
}

//条件查询
const form = ref({
  companyName: "",
  contactTel: "",
  email: ""
});

//每页记录数变化
function chgsize(size) {
  //更改每页记录数
  pageSize.value = size;
  //从每页开始查询
  page.value = 1;
  //查询数据
  initData();
}
const tableData = ref([]);
function chgpg(pg) {
  //更改当前页
  page.value = pg;
  //查询数据
  initData();
}
function update(id) {
  router.push({ path: "/forward/update", query: { id } });
}

//条件查询方法
async function initData() {
  let data = {
    page: page.value,
    pageSize: pageSize.value,
    companyName: form.value.companyName,
    contactTel: form.value.contactTel,
    email: form.value.email
  };
  let res = await queryForward(data);
  console.log(res);
  if (res.code == 200) {
    tableData.value = res.data.data;
    total.value = res.data.total;
    page.value = res.data.page;
    pageSize.value = res.data.pageSize;
  }
}
function query() {
  initData();
}
function reset() {
  form.value.companyName = "";
  form.value.contactTel = "";
  form.value.email = "";
  initData();
}

onMounted(() => {
  initData();
});
</script>

<style scoped>
.container {
  display: flex;
  margin-top: 20px;
  margin-left: 10px;
}
</style>
